package com.das.mechanic_base.mvp.view.finish;

import android.content.Intent;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.Postcard;
import com.alibaba.android.arouter.launcher.ARouter;
import com.das.mechanic_base.R;
import com.das.mechanic_base.R2;
import com.das.mechanic_base.app.X3AppRouterConstants;
import com.das.mechanic_base.app.X3Constent;
import com.das.mechanic_base.base.X3BaseActivity;
import com.das.mechanic_base.mvp.contract.finish.AloneFinishContract;
import com.das.mechanic_base.mvp.presenter.finish.X3AloneFinishPresenter;
import com.das.mechanic_base.utils.SpHelper;
import com.das.mechanic_base.utils.X3StatusBarUtil;
import com.hjq.toast.ToastUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;

public class X3OpenIHSuccessActivity extends X3BaseActivity<X3AloneFinishPresenter> implements AloneFinishContract.View {

    @BindView(R2.id.tv_start)
    TextView tv_start;
    @BindView(R2.id.tv_status)
    TextView tv_status;
    @BindView(R2.id.iv_show)
    ImageView iv_show;

    private long workBaseId;

    private boolean isLoad;

    private Observable<Long> delay;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_x3_open_i_h_success;
    }

    @Override
    protected void initView() {
        X3StatusBarUtil.darkMode(this);
        workBaseId = getIntent().getLongExtra("workBaseId", 0);
        swipeBackLayout.setEnableGesture(false);

    }

    @Override
    protected X3AloneFinishPresenter createPresenter() {
        return new X3AloneFinishPresenter();
    }

    @OnClick({R2.id.tv_back,R2.id.tv_start})
    protected void onViewClick(View view){
        int id = view.getId();
        if (id == R.id.tv_back) {
            EventBus.getDefault().post("UPDATE_CNACEL_ORDER");
            String simple = (String) SpHelper.getData(X3Constent.WORK_MODEL, "SIMPLE");
            Postcard build;
            if ("SIMPLE".equals(simple)) {
                build = ARouter.getInstance().build(X3AppRouterConstants.Router.MAIN_MINI);
            } else {
                build = ARouter.getInstance().build(X3AppRouterConstants.Router.MAIN_LUNCHER);
            }
            build.withFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK).navigation();
        } else if (id == R.id.tv_start) {
            // 开始录屏录音
            isLoad = true;
            viewProxy.showLoading("");
            if (timer != null) {
                timer.cancel();
                timer.start();
            }
            if (mPresenter != null) {
                mPresenter.recordingScreenOnHub(workBaseId);
            }
        }
    }

    @Override
    public void getWorkBaseReportStatus(boolean isSuccess) {

    }

    @Override
    public void openReportOnHubSuccess() {

    }

    @Override
    public void getOpenReportStatusSuccess(int status) {

    }

    @Override
    public void recordingScreenOnHubSuccess() {
        if (mPresenter != null) {
            mPresenter.getRecordingScreenStatus(workBaseId);
        }
    }

    @Override
    public void getRecordingScreenStatusSuccess(int status) {
        if (status == 2) {
            if (viewProxy != null) {
                viewProxy.hideLoading();
            }
            isLoad = false;
            if (timer != null) {
                timer.cancel();
            }
            iv_show.setImageResource(R.mipmap.start_record_icon_success);
            // 成功之后操作布局
            tv_start.setVisibility(View.GONE);
            tv_status.setText(getString(R.string.x3_start_ih_screening));
            return;
        }
        if (status != 2 && isLoad) {
            if (delay == null) {
                delay = Observable.just(workBaseId)
                        .delay(3, TimeUnit.SECONDS);
            }
            delay.subscribe(new Observer<Long>() {
                @Override
                public void onSubscribe(@NonNull Disposable d) {

                }

                @Override
                public void onNext(@NonNull Long integer) {
                    if (mPresenter != null) {
                        mPresenter.getRecordingScreenStatus(workBaseId);
                    }
                }

                @Override
                public void onError(@NonNull Throwable e) {

                }

                @Override
                public void onComplete() {

                }
            });

            return;
        }
    }

    private final CountDownTimer timer = new CountDownTimer(15 * 1000, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {


        }

        @Override
        public void onFinish() {
            if (isFinishing()) {
                return;
            }
            isLoad = false;
            if (viewProxy != null) {
                viewProxy.hideLoading();
            }
            ToastUtils.show(getString(R.string.x3_open_ih_failed));
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (timer != null) {
            timer.cancel();
        }
    }
}